package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.config.*;
import com.cskaoyan.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目二： 配置管理
 */
@RestController
@RequestMapping("admin/config")
public class ConfigController {

    @Autowired
    ConfigService configService;

    //配置管理:商场信息的回显
    @RequestMapping("mall")
    public BaseRespVo mall() {

        MallParam param = new MallParam();
        List<MallInfoData> data = configService.mall();

        param.setMarket_mall_latitude(data.get(0).getKeyValue());
        param.setMarket_mall_name(data.get(1).getKeyValue());
        param.setMarket_mall_qq(data.get(2).getKeyValue());
        param.setMarket_mall_longitude(data.get(3).getKeyValue());
        param.setMarket_mall_phone(data.get(4).getKeyValue());
        param.setMarket_mall_address(data.get(5).getKeyValue());

        return BaseRespVo.ok(param);
    }

    //配置管理：商场信息的修改
    @PostMapping("mall")
    public BaseRespVo updateMall(@RequestBody MallParam param) throws NoSuchFieldException, IllegalAccessException {
//        MallInfoData data = new MallInfoData();
        Field[] fields = param.getClass().getDeclaredFields();
//        //获得变量名、变量值
        int num = 0;
        num = configService.updateMall(param);
        if (num == 0) {
            return BaseRespVo.fail();
        } else return BaseRespVo.ok();
    }

    //运费配置
    @RequestMapping("express")
    public BaseRespVo express() {
        ExpressParam param = new ExpressParam();
        List<MallInfoData> data = configService.express();

        param.setMarket_express_freight_min(data.get(0).getKeyValue());
        param.setMarket_express_freight_value(data.get(1).getKeyValue());
        return BaseRespVo.ok(param);
    }

    @PostMapping("express")
    public BaseRespVo updateExpress(@RequestBody ExpressParam param) {
        Field[] fields = param.getClass().getDeclaredFields();
        int num = 0;
        num = configService.updateExpress(param);
        if (num == 0) {
            return BaseRespVo.fail();
        } else return BaseRespVo.ok();
    }


    //订单配置
    @RequestMapping("order")
    public BaseRespVo order() {
        OrderParam param = new OrderParam();
        List<MallInfoData> data = configService.order();
        param.setMarket_order_comment(data.get(0).getKeyValue());
        param.setMarket_order_unconfirm(data.get(1).getKeyValue());
        param.setMarket_order_unpaid(data.get(2).getKeyValue());
        return BaseRespVo.ok(param);
    }

    @PostMapping("order")
    public BaseRespVo updateOrder(@RequestBody OrderParam param) {
        Field[] fields = param.getClass().getDeclaredFields();
        int num = 0;
        num = configService.updateOrder(param);
        if (num == 0) {
            return BaseRespVo.fail();
        } else return BaseRespVo.ok();
    }

    //小程序配置
    @RequestMapping("wx")
    public BaseRespVo wx() {
        WxParam param = new WxParam();
        List<MallInfoData> data = configService.wx();
        param.setMarket_wx_index_new(data.get(0).getKeyValue());
        param.setMarket_wx_share(data.get(1).getKeyValue());
        param.setMarket_wx_index_hot(data.get(2).getKeyValue());
        param.setMarket_wx_catlog_goods(data.get(3).getKeyValue());
        param.setMarket_wx_catlog_list(data.get(4).getKeyValue());
        param.setMarket_wx_index_brand(data.get(5).getKeyValue());
        param.setMarket_wx_index_topic(data.get(6).getKeyValue());
        return BaseRespVo.ok(param);
    }

    @PostMapping("wx")
    public BaseRespVo updateWx(@RequestBody WxParam param) {
        Field[] fields = param.getClass().getDeclaredFields();
        int num = 0;
        num = configService.updateWx(param);
        if (num == 0) {
            return BaseRespVo.fail();
        } else return BaseRespVo.ok();
    }


}
